<?php
// Copyright (C) 2010 Rod Roark <rod@sunsetsystems.com>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.

require_once("../globals.php");
require_once("$srcdir/acl.inc");
require_once("$srcdir/formdata.inc.php");
require_once("$srcdir/globals.inc.php");
require_once("$srcdir/user.inc");
require_once("$srcdir/classes/CouchDB.class.php");

if ($_GET['mode'] != "user") {
  // Check authorization.
  $thisauth = acl_check('admin', 'super');
  if (!$thisauth) die(xl('Not authorized'));
}

function checkCreateCDB(){
  $globalsres = sqlStatement("SELECT gl_name, gl_index, gl_value FROM globals WHERE gl_name IN 
  ('couchdb_host','couchdb_user','couchdb_pass','couchdb_port','couchdb_dbase','document_storage_method')");
    $options = array();
    while($globalsrow = sqlFetchArray($globalsres)){
      $GLOBALS[$globalsrow['gl_name']] = $globalsrow['gl_value'];
    }
    $directory_created = false;
  if($GLOBALS['document_storage_method'] != 0){
    // /documents/temp/ folder is required for CouchDB
    if(!is_dir($GLOBALS['OE_SITE_DIR'] . '/documents/temp/')){
      $directory_created = mkdir($GLOBALS['OE_SITE_DIR'] . '/documents/temp/',0777,true);      
      if(!$directory_created){
	echo htmlspecialchars( xl("Failed to create temporary folder. CouchDB will not work."),ENT_NOQUOTES);
      }
    }
        $couch = new CouchDB();
    if(!$couch->check_connection()) {
      echo "<script type='text/javascript'>alert('".addslashes(xl("CouchDB Connection Failed."))."');</script>";
      return;
    }
    if($GLOBALS['couchdb_host'] || $GLOBALS['couchdb_port'] || $GLOBALS['couchdb_dbase']){
      $couch->createDB($GLOBALS['couchdb_dbase']);
      $couch->createView($GLOBALS['couchdb_dbase']);
    }
  }
  return true;
}
?>

<html>

<head>
	<?php
	// If we are saving user_specific globals.
	//
	if ($_POST['form_save'] && $_GET['mode'] == "user") {  
	  $i = 0;
	  foreach ($GLOBALS_METADATA as $grpname => $grparr) {
		if (in_array($grpname, $USER_SPECIFIC_TABS)) {
		  foreach ($grparr as $fldid => $fldarr) {
			if (in_array($fldid, $USER_SPECIFIC_GLOBALS)) {
			  list($fldname, $fldtype, $flddef, $flddesc) = $fldarr;
			  $label = "global:".$fldid;
			  $fldvalue = trim(strip_escape_custom($_POST["form_$i"]));
			  setUserSetting($label,$fldvalue,$_SESSION['authId'],FALSE);
			  if ( $_POST["toggle_$i"] == "YES" ) {
				removeUserSetting($label);
			  }
			  ++$i;
			}
		  }
		}
	  }
	}

	// If we are saving main globals.
	//
	if ($_POST['form_save'] && $_GET['mode'] != "user") {
	  /*echo "<pre>";
	  print_r($_POST);
	  echo "</pre>";*/
	  
	  $i = 0;
	  foreach ($GLOBALS_METADATA as $grpname => $grparr) {
		foreach ($grparr as $fldid => $fldarr) {
		  list($fldname, $fldtype, $flddef, $flddesc) = $fldarr;
		  if($fldtype == 'pwd'){
		  $pass = sqlQuery("SELECT gl_value FROM globals WHERE gl_name = '$fldid'");
		  $fldvalueold = $pass['gl_value'];
		  }
		  sqlStatement("DELETE FROM globals WHERE gl_name = '$fldid'");

		  if (substr($fldtype, 0, 2) == 'm_') {
			if (isset($_POST["form_$i"])) {
			  $fldindex = 0;
			  foreach ($_POST["form_$i"] as $fldvalue) {
				$fldvalue = formDataCore($fldvalue, true);
				sqlStatement("INSERT INTO globals ( gl_name, gl_index, gl_value ) " .
				  "VALUES ( '$fldid', '$fldindex', '$fldvalue' )");
				++$fldindex;
			  }
			}
		  }
		  else {
			if (isset($_POST["form_$i"])) {
			  $fldvalue = formData("form_$i", "P", true);
			}
			else {
			  $fldvalue = "";
			}
			if($fldtype=='pwd')
			  $fldvalue = $fldvalue ? SHA1($fldvalue) : $fldvalueold;
			  if(fldvalue){
			  sqlStatement("INSERT INTO globals ( gl_name, gl_index, gl_value ) " .
			  "VALUES ( '$fldid', '0', '$fldvalue' )");
			  }
		  }

		  ++$i;
		}
	  }
	  checkCreateCDB();
	}
	?>

	<!-- supporting javascript code -->
		
	<?php if ($_GET['mode'] == "user") { ?>
	  <title><?php  xl('User Settings','e'); ?></title>
	<?php } else { ?>
	  <title><?php  xl('Global Settings','e'); ?></title>
	<?php } ?>

	<script type="text/javascript">
		var $tabs = $('#tabs').tabs();
		$(".ui-tabs-panel").each(function(i){
		  var totalSize = $(".ui-tabs-panel").size() - 1;
		  if (i != totalSize) {
			  next = i + 2;
			  $(this).append("<a href='#' class='next-tab mover' rel='" + next + "'> &#187;</a>");
		  }
		  if (i != 0) {
			  prev = i;
			  $(this).append("<a href='#' class='prev-tab mover' rel='" + prev + "'>&#171; </a>");
		  }
		});

		$('.next-tab, .prev-tab').click(function() { 
			   $tabs.tabs('select', $(this).attr("rel"));
			   return false;
		});
		   
		function SubmitForm(){		
			
			
			$.ajax({ 
			  type: "POST",
			  cache: false,
			  url: $("#theform").attr('action'), // preview.php
			  data: $("#theform").serialize() + "&form_save=1", // all form fields
			  success: function (data) {
				$("#info").html(data);
			  } // success
			}); // ajax
		}
	</script>

</head>

<body class="body_top">

<?php if ($_GET['mode'] == "user") { ?>
  <form method='post' name='theform' id='theform' action='../super/edit_globals.php?mode=user' onsubmit='return top.restoreSession()'>
<?php } else { ?>
  <form method='post' name='theform' id='theform' action='../super/edit_globals.php' onsubmit='return top.restoreSession()'>
<?php } ?>

<?php if ($_GET['mode'] == "user") { ?>
  <p><b><?php xl('Edit User Settings','e'); ?></b>
<?php } else { ?>
  <p><b><?php xl('Edit Global Settings','e'); ?></b>
<?php } ?>


<div id="tabs">
		
    		<ul>
        		<?php
					$i = 0;
					foreach ($GLOBALS_METADATA as $grpname => $grparr) {
						if ( $_GET['mode'] != "user" || ($_GET['mode'] == "user" && in_array($grpname, $USER_SPECIFIC_TABS)) ) {
							echo " <li><a href='#fragment-".($i+1)."'>" . xl($grpname) . "</a></li>";
							++$i;
						}
					}
				?>
			</ul>
	
			<?php
				$i = 0;
				$k = 1;
				foreach ($GLOBALS_METADATA as $grpname => $grparr) {
				 if ( $_GET['mode'] != "user" || ($_GET['mode'] == "user" && in_array($grpname, $USER_SPECIFIC_TABS)) ) {
				  echo " <div class='ui-tabs-panel' id='fragment-".$k."' style='height:auto;width:97%;'>";

				  echo " <table>";

				  if ($_GET['mode'] == "user") {
				   echo "<tr>";
				   echo "<th>&nbsp</th>";
				   echo "<th>" . htmlspecialchars( xl('User Specific Setting'), ENT_NOQUOTES) . "</th>";
				   echo "<th>" . htmlspecialchars( xl('Default Setting'), ENT_NOQUOTES) . "</th>";
				   echo "<th>&nbsp</th>";
				   echo "<th>" . htmlspecialchars( xl('Set to Default'), ENT_NOQUOTES) . "</th>";
				   echo "</tr>";
				  }

				  foreach ($grparr as $fldid => $fldarr) {
				   if ( $_GET['mode'] != "user" || ($_GET['mode'] == "user" && in_array($fldid, $USER_SPECIFIC_GLOBALS)) ) {
					list($fldname, $fldtype, $flddef, $flddesc) = $fldarr;

					// Most parameters will have a single value, but some will be arrays.
					// Here we cater to both possibilities.
					$glres = sqlStatement("SELECT gl_index, gl_value FROM globals WHERE " .
					  "gl_name = '$fldid' ORDER BY gl_index");
					$glarr = array();
					while ($glrow = sqlFetchArray($glres)) $glarr[] = $glrow;

					// $fldvalue is meaningful only for the single-value cases.
					$fldvalue = count($glarr) ? $glarr[0]['gl_value'] : $flddef;

					// Collect user specific setting if mode set to user
					$userSetting = "";
					$settingDefault = "checked='checked'";
					if ($_GET['mode'] == "user") {
					  $userSettingArray = sqlQuery("SELECT * FROM user_settings WHERE setting_user=? AND setting_label=?",array($_SESSION['authId'],"global:".$fldid));
					  $userSetting = $userSettingArray['setting_value'];
					  $globalValue = $fldvalue;
					  if (!empty($userSettingArray)) {
						$fldvalue = $userSetting;
						$settingDefault = "";
					  }
					}

					echo " <tr title='$flddesc'><td valign='top'><b>$fldname </b></td><td valign='top'>\n";

					if (is_array($fldtype)) {
					  echo "  <select name='form_$i' id='form_$i'>\n";
					  foreach ($fldtype as $key => $value) {
						if ($_GET['mode'] == "user") {
						  if ($globalValue == $key) $globalTitle = $value;
						}
						echo "   <option value='$key'";
						if ($key == $fldvalue) echo " selected";
						echo ">";
						echo $value;
						echo "</option>\n";
					  }
					  echo "  </select>\n";
					}

					else if ($fldtype == 'bool') {
					  if ($_GET['mode'] == "user") {
						if ($globalValue == 1) {
						  $globalTitle = htmlspecialchars( xl('Checked'), ENT_NOQUOTES);
						}
						else {
						  $globalTitle = htmlspecialchars( xl('Not Checked'), ENT_NOQUOTES);
						}
					  }
					  echo "  <input type='checkbox' name='form_$i' id='form_$i' value='1'";
					  if ($fldvalue) echo " checked";
					  echo " />\n";
					}

					else if ($fldtype == 'num') {
					  if ($_GET['mode'] == "user") {
						$globalTitle = $globalValue;
					  }
					  echo "  <input type='text' name='form_$i' id='form_$i' " .
						"size='6' maxlength='15' value='$fldvalue' />\n";
					}

					else if ($fldtype == 'text') {
					  if ($_GET['mode'] == "user") {
						$globalTitle = $globalValue;
					  }
					  echo "  <input type='text' name='form_$i' id='form_$i' " .
						"size='50' maxlength='255' value='$fldvalue' />\n";
					}
					
					else if ($fldtype == 'pwd') {
					  if ($_GET['mode'] == "user") {
						$globalTitle = $globalValue;
					  }
					  echo "  <input type='password' name='form_$i' " .
						"size='50' maxlength='255' value='' />\n";
					}

					else if ($fldtype == 'pass') {
					  if ($_GET['mode'] == "user") {
						$globalTitle = $globalValue;
					  }
					  echo "  <input type='password' name='form_$i' " .
						"size='50' maxlength='255' value='$fldvalue' />\n";
					}

					else if ($fldtype == 'lang') {
					  $res = sqlStatement("SELECT * FROM lang_languages ORDER BY lang_description");
					  echo "  <select name='form_$i' id='form_$i'>\n";
					  while ($row = sqlFetchArray($res)) {
						echo "   <option value='" . $row['lang_description'] . "'";
						if ($row['lang_description'] == $fldvalue) echo " selected";
						echo ">";
						echo xl($row['lang_description']);
						echo "</option>\n";
					  }
					  echo "  </select>\n";
					}

					else if ($fldtype == 'm_lang') {
					  $res = sqlStatement("SELECT * FROM lang_languages  ORDER BY lang_description");
					  echo "  <select multiple name='form_{$i}[]' id='form_{$i}[]' size='3'>\n";
					  while ($row = sqlFetchArray($res)) {
						echo "   <option value='" . $row['lang_description'] . "'";
						foreach ($glarr as $glrow) {
						  if ($glrow['gl_value'] == $row['lang_description']) {
							echo " selected";
							break;
						  }
						}
						echo ">";
						echo xl($row['lang_description']);
						echo "</option>\n";
					  }
					  echo "  </select>\n";
					}

					else if ($fldtype == 'css') {
					  if ($_GET['mode'] == "user") {
						$globalTitle = $globalValue;
					  }
					  $themedir = "$webserver_root/interface/themes";
					  $dh = opendir($themedir);
					  if ($dh) {
						echo "  <select name='form_$i' id='form_$i'>\n";
						while (false !== ($tfname = readdir($dh))) {
						  // Only show files that contain style_ as options
						  //  Skip style_blue.css since this is used for
						  //  lone scripts such as setup.php
						  if (!preg_match("/^style_.*\.css$/", $tfname) || $tfname == 'style_blue.css') { continue; }
						  echo "<option value='$tfname'";
						  if ($tfname == $fldvalue) echo " selected";
						  echo ">";
						  echo $tfname;
						  echo "</option>\n";
						}
						closedir($dh);
						echo "  </select>\n";
					  }
					}

					else if ($fldtype == 'hour') {
					  if ($_GET['mode'] == "user") {
						$globalTitle = $globalValue;
					  }
					  echo "  <select name='form_$i' id='form_$i'>\n";
					  for ($h = 0; $h < 24; ++$h) {
						echo "<option value='$h'";
						if ($h == $fldvalue) echo " selected";
						echo ">";
						if      ($h ==  0) echo "12 AM";
						else if ($h <  12) echo "$h AM";
						else if ($h == 12) echo "12 PM";
						else echo ($h - 12) . " PM";
						echo "</option>\n";
					  }
					  echo "  </select>\n";
					}
					if ($_GET['mode'] == "user") {
					  echo " </td>\n";
					  echo "<td align='center' style='color:red;'>" . $globalTitle . "</td>\n";
					  echo "<td>&nbsp</td>";
					  echo "<td align='center'><input type='checkbox' value='YES' name='toggle_" . $i . "' id='toggle_" . $i . "' " . $settingDefault . "/></td>\n";
					  echo "<input type='hidden' id='globaldefault_" . $i . "' value='" . $globalValue . "'>\n";
					  echo "</tr>\n";
					}
					else {
					  echo " </td></tr>\n";
					}
					++$i;
				   }
				  }
				  echo " </table>\n";
				  echo " </div>\n";
				 }
				 $k++;
				}
				?>
        	

        </div>
<p class="toolbar">
 <input type='button' onclick='SubmitForm();' name='form_save' value='<?php xl('Save','e'); ?>' />
</p>
</center>
</form>
</body>

</html>

